<?
header("Content-type: text/xml");
require_once("engine/www/combustion.php");

$events = $DB->parse('*', 'mod_calendar', 'active = 1 AND (otype = 1 OR otype = 9) AND date > ADDDATE(NOW(), INTERVAL - 60 DAY) ORDER BY flog desc', true);
$artistmeta = $DB->parse_json($events[0]['otype'], "types", "meta")->pageid;
$bioloc = isset($artistmeta) ? $DB->short('name', 'pages', 'ID=' . $artistmeta) : "atom/bio";
$feedfile = $DB->type( "atomcal" );
$file = $DB->parse('flog, nameid', 'db_file', '`type`='.$feedfile );
$file_name = "/var/www/html/" . CLIENT_ROOT . "/DB/files/" . $DB->lang($file['nameid']) . ".atom";
$ldate = $events[0]['flog'];

if ($ldate > $file['flog']) {
	$fh = fopen($file_name, 'w') or die("can't open file");
	$feed = "<?xml version='1.0' encoding='UTF-8' ?>\n";
	$feed .= "<feed xmlns=\"http://www.w3.org/2005/Atom\"\n";
	$feed .= "\txmlns:ev=\"http://purl.org/rss/1.0/modules/event/\">";
	$feed .= "<id>" . engine_get('url') . "/events.rss</id>\n";
	$feed .= "<title>". engine_get('title.control') . "</title>\n";
	$feed .= "<link href=\"" . engine_get('url') . "\" />\n";
	$feed .= "<updated>". date('c', strtotime($events[0]['flog'])) ."</updated>\n";
	foreach ($events as $event) {
		$band = $DB->parse('nameid, active', 'mod_object', 'id='. $event['objid']);
		if ($band['active']) {
			$name = htmlspecialchars($DB->lang($band['nameid']));
			$feed .= "\t<entry>\n";
			$feed .= "\t\t<title type=\"html\">" . $name . "</title>\n";
			$feed .= "\t\t<ev:location type=\"html\">";
			$feed .= htmlspecialchars($DB->lang($DB->short('nameid', 'mod_object', 'ID=' . $event['room'])));
			$feed .= "</ev:location>\n";
			$feed .= "\t\t<category term=\"" . $DB->type_r( $event['otype'] ) . "\"></category>\n";
			$feed .= "\t\t<author><name>" . engine_get('title.control'). "</name></author>\n";
			$feed .= "\t\t<link href='http://" . engine_get('title.control') . "/" . $bioloc . "?id=" . $event['objid'] . "' />\n";
			$feed .= "\t\t<ev:startdate>" . date(c, strtotime($event['date'])) . "</ev:startdate>\n";
			$feed .= "\t\t<updated>" . date(c, strtotime($event['flog'])) . "</updated>\n";
			$feed .= "\t\t<id>tag:" . engine_get('title.control') . "," . date('Y-m-d', strtotime($events[0]['pdate'])) . ":" . $event['ID'] . "</id>\n";
			$feed .= "\t</entry>\n\n";
		}
	}
	$feed .= "</feed>";
	fwrite($fh, $feed);
	fclose($fh);
	$DB->tube("UPDATE db_file SET flog = NOW() WHERE type=" . $feedfile);
} else {
	$fh = fopen($file_name, 'r') or die("can't open file");
	$feed = fread($fh, filesize($file_name));
}
print $feed;
?>


